// 登录成功
// 点击登录时候获取输入框的值
document.querySelector('#btn-login').addEventListener('click', async function (e) {
    e.preventDefault()
    const form = document.querySelector('form')
    const formData = serialize(form, { hash: true, empty: true })
    console.log(formData.username);
    console.log(formData.password);
    // 非空校验
    if (formData.username.trim().length === 0 || formData.password.trim().length === 0) {
        return showTost('账号密码不能为空')
    }
    // 长度校验
    if (formData.username.trim().length < 8 || formData.password.trim().length < 6 ||
        formData.username.trim().length > 30 || formData.password.trim().length > 30) {
        return showTost('账号长度为8-30，密码长度为8-30')
    }
    // try {
    //     const res = await axios.post('login', { ...formData })
    //     showTost(res.data.message)
    //     // 保存token
    //     localStorage.setItem('token', res.data.data.token)
    //     // 存储用户名
    //     localStorage.setItem('username', formData.username)
    //     setTimeout(() => {
    //         location.href = './index.html'
    //     }, 1000)
    // }
    // catch (err) {
    //     showTost(err.response.data.message)
    // }
    const res = await axios.post('login', { ...formData })
    showTost(res.data.message)
    // 保存token
    localStorage.setItem('token', res.data.data.token)
    // 存储用户名
    localStorage.setItem('username', formData.username)
    setTimeout(() => {
        location.href = './index.html'
    }, 1000)
})
